import pymongo

# from settings import mongo_config
from zhipin.settings import mongo_config
from bson.objectid import ObjectId
class MongoDB():
    def __init__(self):
        self.host=mongo_config['host']
        self.port=mongo_config['port']
        self.dbname=mongo_config['db_name']
        self.dbcolection=mongo_config['db_colection']

        self.client=pymongo.MongoClient(host=self.host,port=self.port)

        self.mydb=self.client[self.dbname]
        self.post=self.mydb[self.dbcolection]
        
    def insert(self,data,many=False):
        # 插入数据
       if many==False:
           return self.post.insert_one(data)
       else:
           return self.post.insert_many(data)

    def delete(self,query=None,many=False):
        # 删除数据
        if many==False:
            return self.post.delete_one(query)
        else:
            return self.post.delete_many(query)

    def update(self,query=None,newvalues=None,many=False):
        # 更新数据
        if many==False:
            return self.post.update_one(query,newvalues)
        else:
            return self.post.update_many(query,newvalues)

    def find(self,data=None,limit=None):
        # 查询数据
        if(limit==None):
            return self.post.find(data)
        elif self.is_number(limit):
            return self.post.find(data).limit(limit)
        else:
            return self.post.find_one(data)

    def save(self,data):
        # 保存数据
        return self.post.save(data)

    def drop(self):
        # 删除集合
        return self.post.drop()    

    def is_number(self,s):
        try:
            float(s)
            return True
        except ValueError:
            pass
     
        try:
            import unicodedata
            unicodedata.numeric(s)
            return True
        except (TypeError, ValueError):
            pass
     
        return False


if __name__=='__main__':
    # mongodb=MongoDB()
    # mongodb.insert({'data':'测试写入数据'})


    # 查询全部
    # data = mongodb.find()
    # for x in data:
    #     print(x)


    # 查询一条
    # data2 = mongodb.find({'job_description':'动画设计师'})

    # 查询大于ObjectId 的两条数据
    '''
    data2 = mongodb.find({
        '_id':{
            '$gt':ObjectId("5f2835291902dc861ebedb60")
            }
         },2)
    for x in data2:
        print(x)
    '''

    # 更新数据
    '''
    myquery = { "_id": ObjectId("5f2835291902dc861ebedb61") }
    newvalues = { "$set": { "job_requirements": ["更新数据"] } }
    mongodb.update(myquery,newvalues)

    data3 = mongodb.find({'_id':ObjectId("5f2835291902dc861ebedb61")})
    for x in data3:
        print(x)
    '''

    # 删除数据
    '''
    myquery = { "data": "测试写入数据" }
    data4 = mongodb.delete(myquery)
    print(data4.deleted_count)
    '''

    # 查询全部
    '''
    data = mongodb.find()
    for x in data:
        print(x)
    '''


    # 删除集合
    '''
    collist = mongodb.mydb.list_collection_names()
    # collist = mydb.collection_names()
    if mongodb.dbcolection in collist:   # 判断 sites 集合是否存在
        print("集合已存在！")
    else:
    	print('集合不存在！')

    mongodb.drop()


    collist = mongodb.mydb.list_collection_names()
    # collist = mydb.collection_names()
    if mongodb.dbcolection in collist:   # 判断 sites 集合是否存在
        print("集合已存在！")
    else:
    	print('集合不存在！')
    '''